package com.zang.server.controller;


import com.zang.server.annotation.OptLog;
import com.zang.server.pojo.RespBean;
import com.zang.server.pojo.Salary;
import com.zang.server.service.ISalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.zang.server.constanct.OptTypeConst.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zang
 * @since 2021-12-28
 */
@Api(tags = "工资账套模块")
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {
    @Autowired
    private ISalaryService iSalaryService;
    @ApiOperation(value = "获取所有的工资账套")
    @GetMapping("/")
    public List<Salary> getAllSalaries(){
        return iSalaryService.list();
    }
    @OptLog(optType = SAVE)
    @ApiOperation(value = "添加工资账套")
    @PostMapping("/")
    public RespBean addSalary(@RequestBody Salary salary){
        LocalDateTime now = LocalDateTime.now();
        salary.setCreateDate(now);
        if (iSalaryService.save(salary)){
            return RespBean.success("成功添加工资账套");
        }
        return RespBean.error("添加失败");
    }
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "删除工资账套")
    @DeleteMapping("/{id}")
    public RespBean deleteSalaryByID(@PathVariable("id") Integer sid){
        if (iSalaryService.removeById(sid)){
            return RespBean.success("成功删除工资账套");
        }
        return RespBean.error("删除失败");
    }
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "更新工资账套")
    @PutMapping("/")
    public RespBean updateSalary(@RequestBody Salary salary){
        if (iSalaryService.updateById(salary)){
            return RespBean.success("成功更新工资账套");
        }
        return RespBean.error("更新失败");
    }
}
